<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 评论
 *
 * @author      Eden
 * @datetime    2016/12/9 00:43
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */
class Detail_Model extends API_Model
{
    /**
     * 参数
     * @var array
     */
    private $params = array();

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 主函数
     * @param array $params
     */
    public function run(Array $params)
    {
        $this->params = $params;
        $send['state'] = false;
        do {

            if (empty($params['bbsId'])) {
                $send['error'] = '参数不完整';
                break;
            }

            if (!$this->_checkBbsExists($params['bbsId'])) {
                $send['error'] = '主题不存在';
                break;
            }

            $info = $this->_getBbsDetail($params['bbsId']);
            if (!$info) {
                $send['error'] = '无此主题';
                break;
            }

            @statIncr('pv');

            $send['state'] = true;
            $send['data'] = $info;
        } while(false);
        $this->output->send($send);
    }

    /**
     * 详情
     * @param $bbsId
     * @return bool
     */
    private function _getBbsDetail($bbsId)
    {
        $bbsDb = clone($this->db);
        $query = $bbsDb->where(array('b.status' => 1, 'b.id' => $bbsId))
            ->select('b.id, b.userId, b.content, b.likes, b.comments, b.created, b.source, ba.attachment')
            ->from('bbs as b')
            ->join('bbs_attachment as ba', 'ba.bbsId = b.id')
            ->order_by('b.created', 'DESC')
            ->get();

        $bbsRows = $query->row_array();
        if (empty($bbsRows)) {
            return false;
        }
        $userId = $bbsRows['userId'];

        $row['bbsId'] = intval($bbsRows['id']);
        $row['userId'] = intval($userId);
        $row['content'] = value($bbsRows, 'content');
        $row['likes'] = intval($bbsRows['likes']);
        $row['isLike'] = $this->_ifIsLike($bbsId);
        $row['comments'] = intval($bbsRows['comments']);
        $row['created'] = formateTimeStamp($bbsRows['created']);
        $row['attachment'] = bbsThumb($bbsRows['attachment'], $this->params['type']);
        $row['nickName'] = '';
        $row['avatar'] = '';

        if ($bbsRows['source'] == 'admin') {
            $bbsConfig = $this->_getBbsConfig();
            $avatar = $nickName = '';
            if ($bbsConfig) {
                $avatar = thumb($bbsConfig['thumb'], 100, 100);
                $nickName = $bbsConfig['nickname'];
            }
            $userInfo = [
                'avatar' => $avatar,
                'nickName' => $nickName
            ];
        } else {
            $userInfo = $this->_getUserInfo($userId);
        }
        if ($userInfo) {
            $row = array_merge($row, $userInfo);
        }

        return $row;
    }

    /**
     * 获取设置
     * @return mixed
     */
    private function _getBbsConfig()
    {
        $this->load->model('common/Setting_Model', 'sets');
        return $this->sets->getSetting('system', 'bbs');
    }

    /**
     * 是否点赞过
     * @param int $bbsId
     * @return bool
     */
    private function _ifIsLike($bbsId = 0)
    {
        if (empty($this->params['userId'])) {
            return false;
        }

        $db = clone($this->db);
        $q = $db->get_where('bbs_likes', [
            'userId' => $this->params['userId'],
            'bbsId' => $bbsId
        ]);
        $row = $q->row_array();
        return empty($row) ? false : true;
    }

    /**
     * 获取用户数据
     * @param int $userId
     * @return array
     */
    protected function _getUserInfo($userId)
    {
        $query = $this->db->select('u.id, u.nickName, up.avatar')
            ->from('user as u')
            ->join('user_profile as up', 'up.userId = u.id')
            ->where(array('u.id' => $userId))
            ->get();
        $row = $query->row_array();
        $info = array();
        if ($row) {
            $info['avatar'] = thumb($row['avatar'], 100, 100);
            $info['nickName'] = $row['nickName'];
        }
        return $info;
    }
}
